package org.hq.sihu;

import com.alibaba.druid.support.json.JSONUtils;
import jakarta.xml.bind.DatatypeConverter;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@SpringBootTest
class SihuHtmlTasksTest {
    @Test
    public void SihuGroupSihuHtml() throws IOException {

    }

    /**
     * 首页解析 用于解析标签
     */
    @Test
    public void SihuMenuHome() throws IOException {
        String path = "S:\\work\\TvSeroive\\src\\main\\resources\\static\\home.html";
        Document doc = Jsoup.parse(new File(path), "UTF-8");
        // String uir="https://www.490022c7.com/Enter/home.html";
        //   Document doc = Jsoup.connect(uir).get();

        //定位到一级 菜单
        Elements menus = doc.select("div.menu.clearfix > dl");
        for (Element menut : menus) {
            Element dt = menut.select("dt > a").first();
            String mpath = dt.attr("data-url");
            //过滤图文 菜单
            if (!mpath.equals("/pic/")) {
                // 一级菜单昵称
                String name = baseb64(dt.data());
                System.out.println(name);
                System.out.println(mpath);
                //二级分组
                Elements dd = menut.select("dd > a");
                for (Element dda : dd) {
                    // 二级菜单路径
                    mpath = dda.attr("href");
                    // 二级菜单昵称
                    name = baseb64(dda.data());


                    System.out.println(name);
                    System.out.println(mpath);
                }
            }
        }


    }


    /**
     * 四虎网站分组条目计算拥有多少页面
     */
    @Test
    public void SihugroupItemCount() throws IOException {
        String uir = "https://www.490022c7.com/movie/wuma/";
        Document doc = Jsoup.connect(uir).get();
        //定位 分页按钮 首页
        Elements as = doc.select("div.pc_pagination > a");
        //获取最后一页链接数量
        String href = as.last().attr("href");
        //获取分组分页 数量切割视频地址
        int count = Integer.parseInt(href.substring(href.indexOf("index_") + 6, href.indexOf(".html")));
        System.out.println(count);
        String index;

        // 获取视频条目地址拼接
        for (int i = 1; i <= count; i++) {
            index = null;
            index = href.substring(0, href.indexOf("_" + count + ".html"));
            //首页地址
            if (i == 1) {
                index = index + ".html";
                System.out.println(index);
            }else {
                index = index +"_" + i + ".html";
                System.out.println(index);
            }

        }
       // JSONUtils
    }

    /**
     * 四虎网站html解析 视频条目页面解析
     */
    @Test
    public void SihuHttpPath() throws IOException {
        String path = "K:\\qqtv\\spring\\mybatis\\tvvideo\\src\\main\\resources\\static\\SihuHttpPath.html";

        //   Document doc = Jsoup.parse(new File(path), "UTF-8");
        String uir = "https://www.431c41.com/movie/youma/index_81.html";
        Document doc = Jsoup.connect(uir).get();

        Elements clearfix = doc.getElementsByClass("row col5 clearfix").first().getElementsByTag("dl");
        System.out.println(clearfix.size());
        int i = 0;
        for (Element item : clearfix) {
            i++;
            System.out.println(i);
            //过滤广告链接
            String id = item.attr("id");
            if (!id.equals("listwoBox")) {
                System.out.println("id = " + id);
                //视频路径
                String paths = item.getElementsByTag("a").first().attr("href");
                System.out.println(paths);
                //视频图片路径
                String img = item.getElementsByTag("img").first().attr("data-original");
                System.out.println(img);

                //视频昵称
                String name = item.getElementsByTag("h3").first().data();
                System.out.println("名称 = " + baseb64(name));
            }


        }


    }

    /**
     * * 解析四虎 网站视频播放页面
     * * 获取视频基本信息
     * * 1 uir m3u8播放地址
     * * 2 tmp 视频上传时间
     * * 3 name 视频昵称
     * * 4 视频下载链接
     */
    @Test
    public void siHuVideoMessage() throws IOException {
        String path = "K:\\qqtv\\spring\\mybatis\\tvvideo\\src\\main\\resources\\static\\videom.html";
        //  Document doc = Jsoup.parse(new File(path), "UTF-8");
        String uir = "https://www.3e2ef2eb.com/html/202407/88599.html";
        Document doc = Jsoup.connect(uir).get();

        // 获取 m3u8 链接
        Elements elements = doc.select("script");
        String m3u8Path = null;
        for (Element element : elements) {
            //返回 script 标签 对象数据
            String var = element.html();
            // 查找匹配字符
            Pattern pattern = Pattern.compile("playUrl=\"//\"+");
            Matcher matcher = pattern.matcher(var);
            if (matcher.find()) {
                int iplay = var.indexOf("iplay");
                if (iplay > 0) {
                    //"//"+iplay+"
                    m3u8Path = var.substring(matcher.end() + 8, var.indexOf("\";var posterImg="));

                } else {
                    m3u8Path = var.substring(matcher.end() + 7, var.indexOf("\";var posterImg="));
                }

            }

        }

        System.out.println(m3u8Path);


        // 获取视频信息
        Element pannel = doc.getElementsByClass("pannel clearfix").first();
        // MP4下载地址 download a href
        String download = pannel.getElementsByClass("download").first().getElementsByTag("a").attr("href");
        System.out.println(download);

        // 视频昵称 data
        String name = pannel.getElementsByClass("title").first().data();
        String s = baseb64(name);

        System.out.println(s.substring(3));
        // 获取时间
        String data = pannel.getElementsByClass("data").get(1).text();
        System.out.println(data);

    }

    @Test
    void play() throws IOException {
        String path = "S:\\work\\TvSeroive\\src\\main\\resources\\static\\play1.html";
        Document doc = Jsoup.parse(new File(path), "UTF-8");
        String uir = "https://www.3e2ef2eb.com/html/202407/88599.html";
        //   Document doc = Jsoup.connect(uir).get();

        // 获取 m3u8 链接
        Elements elements = doc.select("script");
        String m3u8Path = null;
        for (Element element : elements) {
            //返回 script 标签 对象数据
            String var = element.html();
            // 查找匹配字符
            Pattern pattern = Pattern.compile("playUrl=\"//\"+");
            Matcher matcher = pattern.matcher(var);
            if (matcher.find()) {
                int iplay = var.indexOf("iplay");
                if (iplay > 0) {
                    //"//"+iplay+"
                    m3u8Path = var.substring(matcher.end() + 8, var.indexOf("\";var posterImg="));

                } else {
                    m3u8Path = var.substring(matcher.end() + 7, var.indexOf("\";var posterImg="));
                }

            }

        }

        System.out.println(m3u8Path);


    }

    /**
     * 四虎网站 视频昵称 baseb64 解析
     * base解码（方法一）
     *
     * @param name 待解码字符串
     * @return 解码字符串
     * @explain DatatypeConverter.java实现
     */
    public static String baseb64(String name) throws UnsupportedEncodingException {
        // 非空字符串才进行解码
        if (name == null && name.length() < 0) return null;
        //切割字符
        String s = name.substring(name.indexOf("('") + 2, name.indexOf("')"));

        // 解码
        byte[] base64Bytes = DatatypeConverter.parseBase64Binary(s);

        // byte[] 转 String
        return new String(base64Bytes, "utf-8");


    }

}